package com.yonyou.pmclouds.completedcatalog.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO;
import com.yonyou.pmclouds.completedcatalog.mapper.CompletedCatalogVOMapper;

/**
 * 竣工资料目录名称重复性校验
 *
 * @author yzx
 * @Date: 2018/5/30 9:51
 */
public class CompletedCatalogNameCheck implements SingleProcessor<CompletedCatalogVO> {

    private CompletedCatalogVOMapper catalogVOMapper;

    public CompletedCatalogNameCheck(CompletedCatalogVOMapper catalogVOMapper) {
        this.catalogVOMapper = catalogVOMapper;
    }

    @Override
    public void process(CompletedCatalogVO vo, CompletedCatalogVO orig) throws BusinessException {
        // 通过名字获取是否已经存在将要保存的目录名称
        CompletedCatalogVO[] catalogVOS = catalogVOMapper.queryByCatalogName(vo.getSname(), RuntimeEnvironment.getTenantId());
        if (catalogVOS.length > 0) {
            // 新增时如果存在同名
            if (vo.getPkCompletedCatalog() == null) {
                throw new BusinessException("已经存在相同的目录名称，请修改!");
            } else {
                // 编辑时除了自己以外同名
                for (CompletedCatalogVO catalogVO : catalogVOS) {
                    if (!vo.getPkCompletedCatalog().equals(catalogVO.getPkCompletedCatalog())) {
                        throw new BusinessException("已经存在相同的目录名称，请修改!");
                    }
                }
            }
        }
    }
}
